import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:freedom/const/app_content.dart';
import 'package:freedom/entity/restful.dart';
import 'package:freedom/page/mahjong/common/mahjong_context.dart';
import 'package:freedom/page/mahjong/common/message_content.dart';
import 'package:freedom/page/mahjong/mahjong_game.dart';
import 'package:freedom/page/mahjong/message/action_message.dart';
import 'package:freedom/util/customer_dialog.dart';
import 'package:freedom/util/http.dart';
import 'package:freedom/util/api_router.dart';

class ChangeCardDialog extends StatelessWidget {
  final MahjongIndexGame game;
  const ChangeCardDialog({super.key, required this.game});

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Material(
        color: Colors.transparent,
        child: Container(
          width: 400.w,
          height: 130.h,
          decoration: BoxDecoration(
            gradient: const LinearGradient(
              colors: [
                Color.fromARGB(0, 31, 30, 30),
                Colors.black45,
                Color.fromARGB(0, 31, 30, 30)
              ],
              begin: Alignment.centerLeft,
              end: Alignment.centerRight,
              stops: [0.0, 0.5, 1.0],
              tileMode: TileMode.clamp,
            ),
            borderRadius: BorderRadius.circular(10),
          ),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              Text(
                "请选择三张同花色的牌与对手交换",
                style: TextStyle(
                    color: Colors.white,
                    fontSize: 15.h,
                    fontFamily: AppContent.fontHyqmd),
              ),
              MaterialButton(
                padding: const EdgeInsets.all(0),
                onPressed: () {
                  //HTTP请求后才关闭
                  var currentHand =
                      MahjongContext.mahjongGameIndex.bottomCardArea!;
                  if (currentHand.HAND_CARD_SELECT.length != 3) {
                    CustomerDialog.showText("请选择三张相同花色牌");
                  } else {
                    var swapCard = currentHand.HAND_CARD_SELECT
                        .map((e) => e.cardCode)
                        .toList();
                    var data = MessageContent<MahjongMessage>(
                            messageFromUid: 1,
                            type: MessageContent.MT_ACT,
                            body: MahjongMessage(
                                type: ActionMessageHandler.EXCHANGE,
                                swapCard: swapCard))
                        .toJson();
                    Http.getInstance().request<Restful>(ApiRouter.roomMessage,
                        contentType: ContentType.json,
                        type: RequestEnum.POST,
                        showErrorMsg: true,
                        data: data, success: (data) {
                      game.overlays.remove(MahjongOverlay.swapCardTipsDialog);
                      for (var value in currentHand.HAND_CARD_SELECT) {
                        value.removeFromParent();
                      }
                    });
                  }
                },
                child: Container(
                  width: 90.h,
                  height: 30.h,
                  alignment: Alignment.center,
                  decoration: BoxDecoration(
                      color: Colors.orange,
                      borderRadius: BorderRadius.circular(5.h)),
                  child: Text(
                    "确定",
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: 15.h,
                      height: 1.0,
                    ),
                  ),
                ),
              )
            ],
          ),
        ),
      ),
    );
  }
}
